﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GLSimulationControl;

namespace _3CMillingSimulator
{
    public partial class MaterialControl : UserControl
    {
        [Browsable( true ), DefaultValue( 10.0f )]
        public float DisplayMargin { get; set; }

        [Browsable( true ), DefaultValue( true ),DisplayName("Point selection enabled")]
        public bool PointSelectionEnabled { get; set; }

        [Browsable( false )]
        public double PointX { get; set; }

        [Browsable( false )]
        public double PointY { get; set; }

        [Browsable( false )]
        public double PointZ { get; set; }

        private double MaterialWidth;

        private double MaterialHeight;

        private double MaterialDepth;

        private Point selectedMousePoint { get; set; }

        private Size selectedMargin { get; set; }

        private List<PointF> points;

        private readonly float leftMarginDraw;

        public MaterialControl()
        {
            InitializeComponent();
            this.DisplayMargin = 20.0f;
            this.points = new List<PointF>();
            this.selectedMargin = new Size( 15, 15 );
            this.PointX = this.PointY = this.PointZ = -1000000;
            this.selectedMousePoint = new Point( -100000, -100000 );
            this.leftMarginDraw = 60;

            if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
            {
                GetMaterialValues();
                this.points.Add( new PointF( 0f, 0f ) );
                this.points.Add( new PointF( (float)this.MaterialWidth, 0 ) );
                this.points.Add( new PointF( 0, (float)this.MaterialHeight ) );
                this.points.Add( new PointF( (float)this.MaterialWidth, (float)this.MaterialHeight ) );
                this.points.Add( new PointF( (float)this.MaterialWidth / 2.0f, (float)this.MaterialHeight / 2.0f ) );
            }
        }

        private void GetMaterialValues()
        {
            this.MaterialWidth = SimulatorControlGL.GetMaterialWidth();
            this.MaterialHeight = SimulatorControlGL.GetMaterialHeight();
            this.MaterialDepth = SimulatorControlGL.GetMaterialDepth();
            this.PointZ = 0;
        }

        private void MaterialControl_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.DrawRectangle( Pens.Black, DisplayMargin, DisplayMargin, this.Width - 2 * this.DisplayMargin - this.leftMarginDraw, this.Height - 2 * this.DisplayMargin );
            foreach (PointF p in this.points)
            {
                int x = (int)(p.X * (this.Width - 2 * this.DisplayMargin - this.leftMarginDraw) / this.MaterialWidth);
                int y = (int)(p.Y * (this.Height - 2 * this.DisplayMargin ) / this.MaterialHeight);
                int mxDiff = Math.Abs( x - selectedMousePoint.X );
                int myDiff = Math.Abs( y - selectedMousePoint.Y );
                if (mxDiff < selectedMargin.Width && myDiff < selectedMargin.Height)
                {
                    g.FillEllipse( Brushes.Red, x - 5 + this.DisplayMargin, y - 5 + this.DisplayMargin, 10, 10 );
                }
                else
                {
                    g.FillEllipse( Brushes.Yellow, x - 5 + this.DisplayMargin, y - 5 + this.DisplayMargin, 10, 10 );
                }
            }

            float ellipseZX = this.Width - this.DisplayMargin - this.leftMarginDraw / 2;
            g.DrawLine( Pens.Black, ellipseZX, this.DisplayMargin, this.Width - this.DisplayMargin - this.leftMarginDraw / 2, this.Height - this.DisplayMargin );
            double ellipseZY =  this.MaterialDepth*(selectedMousePoint.Y)/(this.Height-2*this.DisplayMargin);
            double mzDiff = Math.Abs( ellipseZY - this.PointZ );

            if (mzDiff < selectedMargin.Height)
            {
                g.FillEllipse( Brushes.Red, this.Width - this.DisplayMargin - this.leftMarginDraw / 2 - 5, this.DisplayMargin, 10, 10 );
            }
            else
            {
                g.FillEllipse( Brushes.Yellow, this.Width - this.DisplayMargin - this.leftMarginDraw / 2 - 5, this.DisplayMargin, 10, 10 );
            }
        }

        private void MaterialControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                return;
            this.selectedMousePoint = e.Location;
            this.Refresh();
        }

        private void MaterialControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                return;
            foreach (PointF p in this.points)
            {
                int x = (int)(p.X * (this.Width - 2 * this.DisplayMargin) / this.MaterialWidth);
                int y = (int)(p.Y * (this.Height - 2 * this.DisplayMargin) / this.MaterialHeight);
                int mxDiff = Math.Abs( x - selectedMousePoint.X );
                int myDiff = Math.Abs( y - selectedMousePoint.Y );
                if (mxDiff < selectedMargin.Width && myDiff < selectedMargin.Height)
                {
                    this.PointX = p.X;
                    this.PointY = p.Y;
                    break;
                }
            }
        }
    }
}
